<template>
    <div class="register">
        <h2 class="title">
            Save you account now
        </h2>
        <div class="my-input">
            <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-email"></use>
            </svg>
            <input type="text" v-model="email" placeholder="email" >
        </div>
        <div class="my-input">
            <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-school"></use>
            </svg>
            <input type="text" v-model="account" placeholder="username"  maxlength="8">
        </div>
        <div class="my-input">
            <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-icons-"></use>
            </svg>
            <input type="password" v-model="password" placeholder="password" maxlength="20">
        </div>
        <el-button round @click="register">sign up &nbsp;&nbsp;<i class="el-icon-arrow-right"></i></el-button>
        <div class="footer">
            <p>Already have account?</p>
            <p style="color:rgb(229,163,89);cursor:pointer;" @click.prevent="$emit('toggle')">sign in</p>
        </div>
    </div>
</template>
<script>
export default {
    name: 'register',
    data(){
        return{
            email: '',
            account: '',
            password: '',
        }
    },
    methods:{
         register(){
            var reEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
            if(this.email == ''||this.account == ''||this.password == ''){
                this.$message({
                    message: '心急吃不了热豆腐哦，亲',
                    center: true,
                    type: 'warning'
                })
            }else if(!reEmail.test(this.email)){
                this.$message({
                    message : '邮箱格式错误',
                    center: true,
                    type: 'error'
                })
            }else if(!/^\d{8}$/.test(this.account)){
                this.$message({
                    message : '请输入8位学号',
                    center: true,
                    type: 'error'
                })
            }else if(!/^.{6,20}$/.test(this.password)){
                this.$message({
                    message: '请输入6-20位密码哦',
                    type: 'warning',
                    center: true
                })
            }else{
                this.$http.post('/api/iStudy/register/doRegister.do',{
                    uNo: this.account,
                    uPwd: this.password,
                    uMail: this.email
                }).then(ret => {
                    if(ret.body.status == 0){
                        this.$message({
                            message: '账号已存在',
                            center: true,
                            type: 'error'
                        })
                    }else if(ret.body.status == 2){
                        this.$message({
                            message: '网络错误，请稍后重试',
                            center: true,
                            type: 'error'
                        })
                    }else{
                            this.$message({
                                message: '注册成功，请登录',
                                center: true,
                                type: 'success'
                            })
                            this.$emit('toggle')
                            
    
                        
                    }
                })
            }
        }
    }
}
</script>
<style scoped>
    .register{
        position: absolute;
        left: 20%;
        top: 20%;
        width:400px;
        height: 510px;
    }
    h2{
        line-height: 50px;
        text-align: center;
        margin-bottom: 17%;
        font-weight: 600;
    }
    .my-input{
        width: 80%;
        height: 64px;
        line-height: 64px;
        margin: 0% auto;
        text-align: center;
        border-bottom: 1px solid #dfdfdf;
    }
    .my-input input{
        width: 260px;
        height: 34px;
        line-height: 34px;
        border: 1px solid #fff;
        font-size: 18px;
        text-indent: 1.7em;
    }
    .my-input input:focus{
        outline: none;
    }
    .icon {
        width: 1.3em;
        height: 1.3em;
        vertical-align: -0.2em;
        fill: currentColor;
        overflow: hidden;
    }
    .el-button{
        width: 130px;
        display: block;
        margin: 14% auto 0px;
        background-color: rgb(136,202,201);
        color: #efefef;
        text-align: center;
    }
    .el-button:active{
        border-color: rgb(76, 160, 132);
        color: #fff;
    }
    .footer{
        display: flex;
        justify-content: space-around;
        margin-top: 40px;
    }
</style>>
